﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;

public partial class Admin_SAPFin_SAPFin004b : AdminBasePage
{
    decimal? Id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["Id"] != null)
        {
            Id = decimal.Parse(Request.QueryString["Id"]);
        }
        if (!IsPostBack)
        {
            FinReceipt finReceipt = new FinReceipt();
            if (Id.HasValue)
            {
                if (finReceipt.LoadByPrimaryKey(Id.Value))
                {
                    if (finReceipt.BoxID.HasValue && finReceipt.BoxID.Value > 0)
                    {
                        lbMessage.Text = "此凭证还在凭证盒中，不能修改详细信息！";
                    }
                    else
                    {
                        txtDocumentsNo.Text = finReceipt.DocumentsNo;
                        //txtReference.Text = finReceipt.Reference;
                        txtReferenceKey.Text = finReceipt.ReferenceKey;
                        txtDocumentHeaderText.Text = finReceipt.DocumentHeaderText;
                        txtPostingDate.Value = finReceipt.PostingDate.HasValue ? finReceipt.PostingDate.Value.ToString("yyyy-MM-dd") : "";
                        drpCompany.SelectedValue = finReceipt.CompanyName + "-" + finReceipt.CompanyCode;
                        //drpFunctionDept.SelectedValue = finReceipt.FunctionDept;
                        //drpVoucherDescription.SelectedValue = finReceipt.VoucherDescription;
                        txtDocumentDate.Value = finReceipt.DocumentDate.HasValue ? finReceipt.DocumentDate.Value.ToString("yyyy-MM-dd") : "";
                        txtEntryDate.Value = finReceipt.EntryDate.HasValue ? finReceipt.EntryDate.Value.ToString("yyyy-MM-dd") : "";
                        //txtLogicalSystem.Text = finReceipt.LogicalSystem;
                        txtDocumentSystem.Text = finReceipt.DocumentSystem;
                        txtReferenceTransact.Text = finReceipt.ReferenceTransact;
                        //txtUserName.Text = finReceipt.UserName;
                        txtDocumentsType.Text = Business.getCodeTableValue(finReceipt.DocumentsType);
                        //txtRevesedBy.Text = finReceipt.RevesedBy;
                        //txtComments.Text = finReceipt.Comments;
                        //drpEStorageStatus.SelectedValue = finReceipt.EStorageStatus;
                        //txtEStoreLocation.Text = finReceipt.EStorageLocation;
                    }
                }
            }
            else
            {
                btnSave.Text = "新增";
            }
        }
    }



    protected void btnSave_Click(object sender, EventArgs e)
    {
        FinReceipt finReceipt = new FinReceipt();

        if (Id.HasValue)
        {

            if (finReceipt.LoadByPrimaryKey(Id.Value))
            {
                finReceipt.IsConfirmed = false;
                finReceipt.ConfirmedBy = null;
                finReceipt.ConfirmedDttm = null;
                
                
            }
        }
        else
        {
            finReceipt.CreatedBy= finReceipt.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
            finReceipt.CreatedDttm= finReceipt.UpdatedDttm = DateTime.Now;
        }
        finReceipt.DocumentsNo = txtDocumentsNo.Text;
        finReceipt.Reference = txtReference.Text;
        finReceipt.ReferenceKey = txtReferenceKey.Text;
        finReceipt.DocumentHeaderText = txtDocumentHeaderText.Text;
        if (Business.IsDateTime(txtPostingDate.Value))
        {
            finReceipt.PostingDate = DateTime.Parse(txtPostingDate.Value);
        }
        if (!string.IsNullOrEmpty(drpCompany.SelectedValue))
        {
            finReceipt.CompanyName = drpCompany.SelectedValue.Split('/')[0];
            finReceipt.CompanyCode = drpCompany.SelectedValue.Split('/')[1];
            finReceipt.CompanyNo = Business.getCompanyNo(finReceipt.CompanyName, finReceipt.CompanyCode);
        }
        //finReceipt.FunctionDept=drpFunctionDept.SelectedValue;
        //finReceipt.VoucherDescription=drpVoucherDescription.SelectedValue;
        if (Business.IsDateTime(txtDocumentDate.Value))
        {
            finReceipt.DocumentDate = DateTime.Parse(txtDocumentDate.Value);
        }
        if (Business.IsDateTime(txtEntryDate.Value))
        {
            finReceipt.EntryDate = DateTime.Parse(txtEntryDate.Value);
        }
        finReceipt.LogicalSystem = txtLogicalSystem.Text;
        finReceipt.DocumentSystem = txtDocumentSystem.Text;
        finReceipt.ReferenceTransact = txtReferenceTransact.Text;
        finReceipt.UserName = txtUserName.Text;


        //txtLogicalSystem.Text = finReceipt.LogicalSystem;
        //txtDocumentSystem.Text = ;
        // = ;
        // = ;
        txtDocumentsType.Text = finReceipt.DocumentsType;
        txtRevesedBy.Text = finReceipt.RevesedBy;
        //txtComments.Text = finReceipt.Comments;
        //drpEStorageStatus.SelectedValue = finReceipt.EStorageStatus;
        //txtEStoreLocation.Text = finReceipt.EStorageLocation;

        finReceipt.IsSAP = true;
        finReceipt.IsConfirmed = false;
        finReceipt.Save();
        WebHelper.CloseDialog(true);

    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        WebHelper.CloseDialog(true);
    }

}